const { SIGNAL_DEFINE } = require("./signal");
const clientMgr = require("../clientMgr");

class SeatMgr {
    constructor() {
        this.sits = [];
        this.seatNum = 0;
        this.users = [];    // 用户是可能观战的
        this.gambers = [];  // 赌徒是参与游戏的
        this.exitAfterEnd = [];
        this.signal = null;
    }

    setSignal(signal) {
        this.signal = signal;
    }

    setSeatNum(seatNum) {
        this.seatNum = seatNum;
        for (let i = 0; i < seatNum; ++i) {
            this.users[i] = {};
            this.gambers[i] = {};
        }
    }

    setRoomInfo(roomInfo) {
        var seats = roomInfo.seats;
        for (let i = 0; i < seats.length; ++i) {
            if (seats[i].userId < 0) continue;
            this.users[i] = seats[i];
            if (seats[i].sit == 0) continue;

            var data = this.gambers[i] = {};
            data.seatIndex = i;
            data.sit = seats[i].sit;
            data.userId = seats[i].userId;
            //持有的牌
            data.holds = [];
            data.score = 0;
            data.scoreBetting = 0;
        }

    }

    refreshOnlineUser() {
        for (let i = 0; i < this.exitAfterEnd.length; ++i) {
            var userId = this.exitAfterEnd[i];
            var seatIndex = this.getSeatIndexByUserId(userId);
            if (seatIndex) {
                delete this.users[seatIndex];
                delete this.gambers[seatIndex];
                this.signal.emit(SIGNAL_DEFINE.USER_EXIT, userId);
            }
        }
        this.exitAfterEnd = [];

        for (let i = 0; i < this.users.length; ++i) {
            if (this.users[i] && !clientMgr.isOnline(this.users[i].userId)) {
                this.signal.emit(SIGNAL_DEFINE.USER_EXIT, this.users[i].userId);
                this.users[i] = null;
                this.gambers[i] = null;
            }
        }
    }

    isUserSitBySeatIndex(seatIndex) {
        return users[seatIndex].sit;
    }

    sitDown(userId, value) {
        var user = this.getSeatUser(userId);
        var index = this.getSeatIndexByUserId(userId);
        user.sit = (value ? 1 : 0);
        if (value) {
            var data = this.gambers[index] = {};
            data.seatIndex = index;
            data.sit = user.sit;
            data.userId = user.userId;
            //持有的牌
            data.holds = [];
            data.score = 0;
            data.scoreBetting = 0;
        } else {
            delete this.gambers[index];
        }
        this.signal.emit(SIGNAL_DEFINE.USER_SIT, userId, value);
    }

    getSeatUser(userId) {
        for (let i = 0; i < this.users.length; ++i) {
            if (this.users[i].userId == userId) {
                return this.users[i];
            }
        }
    }

    getSitUsers() {
        var sitUsers = [];
        for (let i = 0; i < this.users.length; ++i) {
            if (this.users[i].sit == 1) {
                sitUsers[i] = this.users[i];
            }
        }
        return sitUsers;
    }

    getUsers() {
        return this.users;
    }

    getSitUsersId() {
        var sitUsersId = [];
        for (let i = 0; i < this.users.length; ++i) {
            if (this.users[i].sit == 1) {
                sitUsersId.push(this.users[i].userId);
            }
        }
        return sitUsersId;
    }

    getGamberByUserId(userId) {
        for (let i = 0; i < this.gambers.length; ++i) {
            if (this.gambers[i].userId == userId) {
                return this.gambers[i];
            }
        }
    }

    getSeatIndexByUserId(userId) {
        for (let i = 0; i < this.users.length; ++i) {
            if (this.users[i].userId == userId) {
                return i;
            }
        }
    }

    getGamberNum() {
        return this.getGamberIndexs().length;
    }

    getUserIdBySeatIndex(seatIndex) {
        return this.users[seatIndex].userId;
    }

    getGamberBySeatIndex(seatIndex) {
        return this.gambers[seatIndex];
    }

    getGambers() {
        return this.gambers;
    }

    getGamberIndexs() {
        var indexArr = [];
        for (let i = 0; i < this.gambers.length; ++i){
            if (this.gambers[i]) {
                indexArr.push(i);
            }
        }
        return indexArr;
    }

    getGamberIndexsByBanker(banker) {
        var indexArr = [];
        for (let i = 0; i < this.gambers.length; ++i){
            var index = (i + banker) % this.seatNum;
            if (this.gambers[index]) {
                indexArr.push(index);
            }
        }
        return indexArr;
    }

    getNextGamber(gamberIndex) {
        for (let i = 1; i < this.gambers.length; ++i) {
            var index = (i + gamberIndex) % this.gambers.length;
            if (this.gambers[index]) return index;
        }
        return gamberIndex;
    }
}

exports.SeatMgr = SeatMgr;